<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/12/3
 * Time: 16:22
 */

namespace app\common\mysic\WxError;

use app\common\mysic\WxRefundErrorResponseHandle;

class RefundTimesExceeded extends WxRefundErrorResponseHandle implements WxResponseErrorHandlerInterface
{
    protected function updateReceivesTimes()
    {
        $this->receivesEntity->times = 46;
        return $this->receivesEntity->isUpdate(true)->save();
    }

    public function handle()
    {
        try {
            $this->log->info('开始', '更新分红主表分红次数到赏金分红初始状态');
            $ret = $this->updateReceivesTimes();
            $this->log->info(bool2str($ret), '更新分红主表分红次数到最大值结果');
        }catch(\Throwable $t) {
            $this->log->error(exceptionLogFormat($t), '更新分红主表分红次数到赏金分红初始状态抛出异常');
            return false;
        }
        try{
            $this->log->info('开始', '分红转为赏金');
            $ret = $this->dealUserAccountOp
                ->setBonus($this->bonus)
                ->setUserEntity($this->dealUserEntity)
                ->setUserCapitalModel($this->dealUserCapitalModel)
                ->transfer();
            $this->log->info(bool2str($ret), '分红转为赏金结果');
        } catch(\Throwable $t) {
            $this->log->error(exceptionLogFormat($t), '更新商户分账记录');
            return false;
        }
        if($this->bonus > 0) {
            $this->log->info('开始', '更新分红记录副表');
            try {
                $ret = $this->bonusReceiveUserOp
                    ->setBonusSetting($this->bonusSettingEntity)
                    ->setRefundType(2)
                    ->setOtherOrder($this->paidUserOrderEntity)
                    ->setRefundData('')
                    ->setBonus($this->bonus)
                    ->setReceiveEntity($this->receivesEntity)
                    ->setModel($this->bonusReceiveUserModel)
                    ->append();
                $this->log->info(bool2str($ret), '更新分红记录副表结果');
            } catch (\Throwable $e) {
                $this->log->error(exceptionLogFormat($e), '更新分红记录副表时抛出异常');
                $this->log->info('', '业务结束');
                return false;
            }
        }
    }
}